package com.rc.evcharger.service.elec;

import com.rc.evcharger.mapper.elec.ElecGunMapper;
import com.rc.evcharger.model.elec.ElecGun;
import com.rc.evcharger.model.elec.ElecGunExample;
import com.rc.evcharger.model.elec.ElecPile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class ElecGunServiceImpl implements ElecGunService {

    @Autowired
    private ElecGunMapper elecGunMapper;

    @Override
    public ElecGunMapper getMapper() {
        return elecGunMapper;
    }

    @Override
    public ElecGun selectGunByGunNo(String gunNo) {
        ElecGunExample elecGunExample = new ElecGunExample();
        elecGunExample.createCriteria().andGunNoEqualTo(gunNo);
        List<ElecGun> list = elecGunMapper.selectByExample(elecGunExample);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public ElecGun selectGunBypileRtua(String pileRtua) {
        ElecGunExample elecGunExample = new ElecGunExample();
        elecGunExample.createCriteria().andPileRutaEqualTo(pileRtua);
        List<ElecGun> list = elecGunMapper.selectByExample(elecGunExample);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public ElecGun validGun(ElecPile pile, String gunNum) {
        ElecGunExample gunExample = new ElecGunExample();
        gunExample.createCriteria().andElecPileIdEqualTo(pile.getElecPileId());
        List<ElecGun> guns = elecGunMapper.selectByExample(gunExample);
        if (CollectionUtils.isEmpty(guns)) {
            return null;
        }

        if (gunNum.length() > 1) {
            gunNum = gunNum.substring(1);
        }
        for (ElecGun g : guns) {
            if (g.getGunNum().endsWith(gunNum))
                return g;
        }
        return guns.get(0);
    }


    @Override
    public List<ElecGun> listByElecPileId(Long elecPileId) {
        ElecGunExample gunExample = new ElecGunExample();
        gunExample.createCriteria().andElecPileIdEqualTo(elecPileId);
        List<ElecGun> guns = elecGunMapper.selectByExample(gunExample);
        if (CollectionUtils.isEmpty(guns)) {
            return null;
        }
        return guns;
    }

    @Override
    public void updateTableByHongYY(String tableName) {
        elecGunMapper.updateTableByHongYY(tableName);
    }
}
